//
//  XQFourTableViewCell.swift
//  XiaoQiQiaoSwift
//
//  Created by tech-lab on 2016/11/28.
//  Copyright © 2016年 校企桥. All rights reserved.
//

import UIKit


class ThreeCollectViewCell: UICollectionViewCell {

    var titleLabel = UILabel()
    var salaryLabel = UILabel()
    var cityLabel = UILabel()
    var typeLabel = UILabel()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        InitUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var positionMode:XQHomePositionMode?{
        didSet{
            titleLabel.text = positionMode?.position_name
            salaryLabel.text = positionMode?.salary
            cityLabel.text = positionMode?.area_id
            typeLabel.text = positionMode?.position_type
        }
    }
    
    
    func InitUI(){
        self.contentView.layer.borderWidth = 0.3;
        self.contentView.layer.borderColor = UIColor.lightGray.cgColor
        
        
        titleLabel = UILabel.init(frame: CGRect.zero)
        titleLabel.font = UIFont.systemFont(ofSize: 14)
        self.contentView .addSubview(titleLabel)
        
        salaryLabel = UILabel.init(frame: CGRect.zero)
        salaryLabel.font = UIFont.systemFont(ofSize: 12)
        self.contentView .addSubview(salaryLabel)

        cityLabel = UILabel.init(frame: CGRect.zero)
        cityLabel.font = UIFont.systemFont(ofSize: 12)
        self.contentView .addSubview(cityLabel)

        typeLabel = UILabel.init(frame: CGRect.zero)
        typeLabel.font = UIFont.systemFont(ofSize: 10)
        typeLabel.backgroundColor = UIColor.orange
        typeLabel.layer.cornerRadius = 3
        self.contentView .addSubview(typeLabel)
        
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView.snp.left).offset(5)
            make.top.equalTo(self.contentView.snp.top).offset(5)
            make.right.equalTo(self.contentView.snp.right).offset(-5)
        }
        
        salaryLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView.snp.left).offset(5)
            make.bottom.equalTo(self.contentView.snp.bottom).offset(-5)
        }
        
        let line = UIView.init(frame: CGRect.zero)
        line.backgroundColor = UIColor.lightGray
        self.contentView.addSubview(line)
        
        line.snp.makeConstraints { (make) in
            make.left.equalTo(salaryLabel.snp.right).offset(2)
            make.width.equalTo(0.5)
            make.height.equalTo(15)
            make.centerY.equalTo(salaryLabel.snp.centerY)
        }
        
        cityLabel.snp.makeConstraints { (make) in
            make.left.equalTo(line.snp.right).offset(3)
            make.bottom.equalTo(self.contentView.snp.bottom).offset(-5)
        }
        
        typeLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self.contentView.snp.right).offset(-5)
            make.bottom.equalTo(self.contentView.snp.bottom).offset(-5)
            
        }
        
        
        
    }
    
    
    
    
    
}



class XQHomeFourTableViewCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource  {

    var dataArray = [XQHomePositionMode]()
    public var homeself = XQHomeViewController()
    
    var collectionView:UICollectionView!
    private let ThreeCollectViewCellID = "ThreeCollectViewCellID"
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        InitUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func InitUI() {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize.init(width: SCREEN_WIDTH/2, height: 40)
        
        collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 120), collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.bounces = true
        collectionView.backgroundColor = UIColor.white
        self.contentView.addSubview(collectionView)
        
        self.collectionView.register(ThreeCollectViewCell.self, forCellWithReuseIdentifier: ThreeCollectViewCellID)
        

    
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ThreeCollectViewCellID, for: indexPath) as!ThreeCollectViewCell
        cell.positionMode = dataArray[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let detailVc = XQPositionDetailViewController()
        let positonMode = dataArray[indexPath.row]
        detailVc.pid = (positonMode.id)!
        homeself.navigationController?.pushViewController(detailVc, animated: true)

    }
    


}
